package com.xinlong.controller;


import com.xinlong.entity.Permission;
import com.xinlong.entity.Role;
import com.xinlong.entity.Userinfo;
import com.xinlong.service.IuserService;
import org.apache.ibatis.annotations.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/user")
public class Usercontroller {

    @Autowired
    IuserService iuserService;


    @RequestMapping("/main.do")
    public ModelAndView main(String username, HttpSession session){
        ModelAndView mv=new ModelAndView();
        Userinfo findbyname = iuserService.findbyname(username);

/*
        findbyname.getRoles().get(0)
                .getPermissions()
                .get(0).getId();
        */
        mv.setViewName("main");
        List<Role> roles =  findbyname.getRoles();
        List<Permission> permissions = new ArrayList<>();
        for(Role role:roles){
            List<Permission> pers = role.getPermissions();
            for(Permission temp:pers){

                if(!permissions.contains(temp)){
                    permissions.add(temp);
                }
            }
        }
        findbyname.setPermissions(permissions);


        session.setAttribute("UserList",findbyname);



        return mv;
    }

    @RequestMapping("/addRoleToUser.do")
    public String addRoleToUser(@RequestParam(name = "userId") String userId, @RequestParam(name = "ids")String[] ids){

        iuserService.addRoleToUser(userId,ids);
        return "redirect:findAll.do";
    }


    @RequestMapping("/findUserByIdAndAllRole.do")
    public ModelAndView findUserByIdAndAllRole(String id){
        ModelAndView mv=new ModelAndView();
        Userinfo userinfo=iuserService.findbyid(id);
        List<Role> list=iuserService.notUserbyRoleid(id);
        mv.addObject("user",userinfo);
        mv.addObject("roleList",list);
        mv.setViewName("user-role-add");
        return mv;
    }


    @RequestMapping("/findAll.do")
    public ModelAndView SelectUser(){
        ModelAndView mv=new ModelAndView();
        List<Userinfo> userinfos= iuserService.SelectUser();
        mv.addObject("userList",userinfos);
        mv.setViewName("user-list");
        return mv;
    }
    @RequestMapping("/save.do")
    public String save(Userinfo userinfo){
        try{

            iuserService.save(userinfo);
        }catch (Exception e){

        }

        return "redirect:findAll.do";
    }

    @RequestMapping("/findById.do")
    public  ModelAndView findbyid(String id){
        ModelAndView mv=new ModelAndView();
        Userinfo userinfo=iuserService.findbyid(id);
        mv.addObject("user",userinfo);
        mv.setViewName("user-show1");
        return mv;
    }

}
